package com.levy.contronller.lamdaTest.methodrefenrence;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

public class MethodReferenceTest {

    public String getString(Supplier<String> supplier) {
        return supplier.get() + "test";
    }
    public String getString2(String str, Function<String,String> function) {
        return function.apply(str);
    }

    public static void main(String[] args) {
//        List<Integer> list = Arrays.asList(1, 2, 5, 8, 9, 51);
//        list.forEach(o -> System.out.println(o));
//        list.forEach(System.out::println);
//

        List<Stu> list1 = Arrays.asList(
                new Stu("zhangsan", 90),
                new Stu("lisi", 40),
                new Stu("wangwu", 48),
                new Stu("zhaoliu", 10)
        );
        list1.sort((s1, s2) -> Stu.compareScore(s1, s2));
        list1.forEach(o -> System.out.println(o));
        //或者  类名::静态方法引用
        list1.sort(Stu::compareScore);
        list1.forEach(o -> System.out.println(o));

        //或者  引用名（对象名）::实例化引用
        Stu stu = new Stu();
        list1.sort(stu::compareName1);
        list1.forEach(o -> System.out.println(o));

        //或者  类名::实例方法名 引用
        list1.sort(Stu::compareName2);
        list1.forEach(o -> System.out.println(o));

        List<String> citys = Arrays.asList("gz", "cq", "sz", "zh");
        Collections.sort(citys, (c1, c2) -> c1.compareToIgnoreCase(c2));
        Collections.sort(citys, String::compareToIgnoreCase);
        citys.sort(String::compareToIgnoreCase);

        //或者构造方法引用 类名::new
        MethodReferenceTest methodReferenceTest = new MethodReferenceTest();
        System.out.println(methodReferenceTest.getString(String::new));

        System.out.println(methodReferenceTest.getString2("HELLO",String::new));


    }


}


class Stu {
    private String name;
    private Integer score;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getScore() {
        return score;
    }

    public void setScore(Integer score) {
        this.score = score;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("{");
        sb.append("\"name\":\"")
                .append(name).append('\"');
        sb.append(",\"score\":")
                .append(score);
        sb.append('}');
        return sb.toString();
    }

    public Stu() {
    }

    public Stu(String name, Integer score) {
        this.name = name;
        this.score = score;
    }


    //方法里面并没有用到该类中的属性，即使剥离该类，放在其他地方也可以运行，
    //从设计角度考虑，这是错误的写法
    public static int compareScore(Stu stu1, Stu stu2) {
        return stu1.getScore() - stu2.getScore();
    }

    public int compareName1(Stu stu1, Stu stu2) {
        return stu1.name.compareToIgnoreCase(stu2.name);
    }

    public int compareName2(Stu stu) {
        return this.getName().compareToIgnoreCase(stu.getName());
    }
}